// packageD/auction/auctioneer-publish/publish_second/publish_second.js
var app = getApp();
import util from '../../../../utils/util.js';
// var datime = util.formatTime(new Date());
import dateUtils from '../../../../utils/dateutils.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    endorsement_name: '',
    params_json: [],
    onetap: true,
    twotap: true,
    date: new Date().getTime(),
    isShowPicker: false,
    isShowPicker_two: false,
    // json
    form: {
      market_price: '',
      mini_markup: '',
      // mini_deal: '',
      prepayment_mode: '1',
      advance_proportion: '',
      advance_money: '',
      start_shooting: '',
      end_shooting: '',
      duration: '',
      endorsement: '',
      start_price: '',
      is_restart: '0',
      em_proportion: '',
      em_mode: '1',
      fixed_price: '',
      delay_before_minute:'',
      delay_cycle_minute:''
    },
    show: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.json) {
      let json = JSON.parse(options.json);
      this.setData({
        params_json: json,
      });
    }
    if (options.goods_id) {
      this.setData({
        goods_id: options.goods_id
      });
      this.getInfo();
    }
    this.getAgreement();
    console.log(this.data.params_json);
  },
  back() {
    wx.navigateBack({
      delta: 2
    });
  },
  // 日期组件
  onYMDhms(e) {
    this.setData({
      isShowPicker: true,
      mode: "YMDhm",
      data: {
        type: "YMDhm"
      }
    });
  },
  // 日期组件二
  onYMDhms_two(e) {
    this.setData({
      isShowPicker_two: true,
      mode: "YMDhm",
      data: {
        type: "YMDhm"
      }
    });
  },
  datePickerCancellEvent(e) {
    this.setData({
      isShowPicker: false,
      isShowPicker_two: false
    });
  },
  datePickerOkEvent(e) {
    let date = e.detail.date;
    // var timestamp = Date.parse(new Date());
    // var starTime = Date.parse(dateUtils.formatLongTime(date, "Y/M/D h:m"));
    let isshowtime = dateUtils.formatLongTime(date, "Y/M/D h:m");
    // if(Number(starTime)<Number(timestamp)){
    //   wx.showToast({
    //     title: '请输入正确的开拍时间',
    //     duration:1000,
    //     icon:'none'
    //   })
    //   return
    // }
    this.setData({
      isShowPicker: false,
      "form.start_shooting": dateUtils.formatLongTime(date, "Y/M/D h:m"),
      isshowtime1: isshowtime
    });
    console.log(Date.parse(this.data.form.start_shooting));
  },
  datePickerOkEvent_two(e) {
    console.log(Date.parse(this.data.form.start_shooting));
    var timestamp = Date.parse(new Date());
    let date = e.detail.date;
    var starTime = Date.parse(this.data.form.start_shooting);
    var endTime = Date.parse(dateUtils.formatLongTime(date, "Y/M/D h:m"));
    let isshowtime = dateUtils.formatLongTime(date, "Y/M/D h:m");
    console.log('------------------------', endTime, timestamp);
    if (Number(starTime) >= Number(endTime)) {
      wx.showToast({
        title: '结束时间要大于开始时间',
        icon: 'none',
        duration: 1000
      });
      return;
    }
    this.setData({
      isShowPicker_two: false,
      "form.end_shooting": dateUtils.formatLongTime(date, "Y/M/D h:m"),
      isshowtime2: isshowtime
    });
  },
  lastStep() {
    wx.navigateBack({
      // url: '/packageD/auction/auctioneer-publish/publish_first/publish_first',
      delta: 1
    });
  },
  is_onetap() {
    this.setData({
      onetap: !this.data.onetap
    });
  },
  is_twotap() {
    this.setData({
      twotap: !this.data.twotap
    });
  },
  line1(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.market_price": keyValue
    });
  },
  line2(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.start_price": keyValue
    });
  },
  line3(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.fixed_price": keyValue
    });
  },
  line4(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.mini_markup": keyValue
    });
  },
  line5(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.mini_deal": keyValue
    });
  },
  line6(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.time_interval": keyValue
    });
  },
  line7(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.duration": keyValue
    });
  },
  line8(e) {
    let keyValue = e.detail.value;
    let keyId = e.currentTarget.dataset.id;
    console.log(keyId, keyValue);
    if (keyId == '1') {
      this.setData({
        "form.prepayment_mode": keyId,
        "form.advance_proportion": keyValue
      });
    } else if (keyId == '2') {
      this.setData({
        "form.prepayment_mode": keyId,
        "form.advance_money": keyValue
      });
    }
    console.log(this.data.form.prepayment_mode, this.data.form.advance_proportion);
  },
  line9(e) {
    let keyValue = e.detail.value;
    let keyId = e.currentTarget.dataset.id;
    console.log(keyId, keyValue);
    if (keyId == '1') {
      this.setData({
        "form.em_mode": keyId,
        "form.em_proportion": keyValue
      });
    } else if (keyId == '2') {
      this.setData({
        "form.em_mode": keyId,
        "form.endorsement": keyValue
      });
    }
    // console.log(this.data.form.em_proportion,this.data.form.endorsement)
  },
  tap_delay_before_minute(e){
    let num = e.detail.value;
    if(e.currentTarget.dataset.key == "delay"){
      this.setData({
        "form.delay_before_minute":num
      });
    }else{
      this.setData({
        "form.delay_cycle_minute":num
      });
    }
  },
  line10(e) {
    let keyValue = e.detail.value;
    this.setData({
      "form.dispatch_price": keyValue
    });
  },
  eInvoiceChange(e) {
    let val = e.detail;
    this.setData({
      "form.is_restart": val
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 发布获取信息
  getAgreement() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            is_examine: res.data.getAgreement.is_examine,
            endorsement_name: res.data.endorsement_name
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 编辑接口
  getInfo() {
    var that = this;
    console.log(this.data.goods_id);
    let json = {
      art: 'editGoods',
      goods_id: this.data.goods_id
    };
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            "form.advance_money": res.data.editGoods.has_one_auction_goods.advance_money,
            "form.advance_proportion": res.data.editGoods.has_one_auction_goods.advance_proportion,
            "form.market_price": res.data.editGoods.market_price,
            "form.endorsement": res.data.editGoods.has_one_auction_goods.endorsement,
            "form.em_proportion": res.data.editGoods.has_one_auction_goods.em_proportion,
            // "form.mini_deal": res.data.editGoods.has_one_auction_goods.mini_deal,
            "form.mini_markup": res.data.editGoods.has_one_auction_goods.mini_markup,
            "form.start_price": res.data.editGoods.has_one_auction_goods.start_price,
            "form.start_shooting": util.formatTimeTwo(res.data.editGoods.has_one_auction_goods.start_shooting, 'Y/M/D h:m:s'),
            "form.end_shooting": util.formatTimeTwo(res.data.editGoods.has_one_auction_goods.end_shooting, 'Y/M/D h:m:s'),
            "form.fixed_price": res.data.editGoods.has_one_auction_goods.fixed_price,
            "form.duration": res.data.editGoods.has_one_auction_goods.duration,
            "form.time_interval": res.data.editGoods.has_one_auction_goods.time_interval,
            twotap: app._isTextEmpty(res.data.editGoods.has_one_auction_goods.endorsement, ) ? true : false,
            onetap: app._isTextEmpty(res.data.editGoods.has_one_auction_goods.advance_money) ? true : false,
            "form.dispatch_price": res.data.editGoods.has_one_auction_goods.dispatch_price,
            isshowtime1: util.formatTimeTwo(res.data.editGoods.has_one_auction_goods.start_shooting, 'Y/M/D h:m:s'),
            isshowtime2: util.formatTimeTwo(res.data.editGoods.has_one_auction_goods.end_shooting, 'Y/M/D h:m:s'),
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 发布接口
  showPop() {
    let that = this;
    let json = Object.assign(that.data.params_json.from, that.data.form);
    if (!app._isTextEmpty(json.content)) {
      json.content = `<img src="${json.content}" style="width:100%;"><br />`;
    }
    let arrcategory = [];
    arrcategory[0] = JSON.parse(that.data.params_json.category)[0][0];
    arrcategory[1] = JSON.parse(that.data.params_json.category)[0][1];
    if(JSON.parse(that.data.params_json.category)[0][2]){
      arrcategory[2] = JSON.parse(that.data.params_json.category)[0][2];
    }
    json.param_title = JSON.parse(that.data.params_json.param_title);
    json.param_value = JSON.parse(that.data.params_json.param_value);
    json.category = arrcategory;
    json.thumb_url = JSON.stringify(json.thumb_url);
    json.art = 'releaseGoods';
    json.start_shooting = String(new Date(json.start_shooting).getTime()).substring(0, 10);
    json.end_shooting = String(new Date(json.end_shooting).getTime()).substring(0, 10);
    json.prepayment_mode == 2 ? delete json.advance_proportion : delete json.advance_money;
    json.em_mode == 2 ? delete json.em_proportion : delete json.endorsement;
    delete json.em_mode;

    if (!json.start_shooting || json.start_shooting == 'NaN') {
      app.tips("请选择开始时间");
      return;
    }
    if (!json.end_shooting || json.end_shooting == 'NaN') {
      app.tips("请选择结束时间");
      return;
    }
    if (Number(json.start_shooting) >= Number(json.end_shooting)) {
      app.tips("开始时间不能大于结束时间");
      return;
    }

    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (that.data.is_examine) {
            that.setData({
              show: true
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});